/**
 *
 */
package de.vl5.propellerhead.meat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author dt0b85
 *
 */
public class MeasureSet {



	private static final String SEPARATOR = ";";
	private List<MeasureValueCollector> measurements;
	private int rotorSteps;
	private int measureAverageAmount;
	private String ignoreAxis;
	private int measureAverageTrash;
	private long measureDuration;

	public MeasureSet(int aMeasureAverageAmount, int aMeasureAverageTrash, String aIgnoreAxis, int aRotorSteps, long aMeasureDuration) {
		rotorSteps = aRotorSteps;
		measureDuration = aMeasureDuration;
		measureAverageAmount = aMeasureAverageAmount;
		measureAverageTrash = aMeasureAverageTrash;
		ignoreAxis = aIgnoreAxis;
		measurements = new ArrayList<MeasureValueCollector>();
		measurements.add(new MeasureValueCollector(measurements.size(), measureAverageAmount, measureAverageTrash, ignoreAxis, System.currentTimeMillis()));
	}

	public MeasureValueCollector getCurrentCollector() {
		return measurements.get(measurements.size() - 1);
	}

	public void setCurrentCollector(MeasureValueCollector aCollector) {
		measurements.add(aCollector);
	}

	public int getFrequency() {
		long tempTime = System.currentTimeMillis() - getCurrentCollector().getMeasureStartTime();
		tempTime = tempTime / 1000;
		return (int) (getCurrentCollector().getSize() / tempTime);
	}

	public int getBestMeasure() {
		int tempPosition = 0;
		float tempBestMeasureValue = 9999;
		for (int i = 0; i < measurements.size(); i++) {
			if (measurements.get(i).getGap() < tempBestMeasureValue) {
				tempBestMeasureValue = measurements.get(i).getGap();
				tempPosition = i;
			}
		}

		return tempPosition + 1;
	}

	public String getTendency() {
		if (measurements == null || measurements.size() < 2) {
			return "";
		}

		return getCurrentCollector().getTendency(measurements.get(measurements.size() - 2));
	}

	public boolean isFinish() {
		return measurements.size() == rotorSteps;
	}

	public void startNewMeasurement() {
		measurements.add(new MeasureValueCollector(measurements.size(), measureAverageAmount, measureAverageTrash, ignoreAxis, System.currentTimeMillis()));
	}

	public int getCurrentStep() {
		return measurements.size();
	}

	public MeasureValueCollector getLastMeasurement() {
		if (measurements != null && measurements.size() > 1) {
			return measurements.get(measurements.size()-2);
		}
		return null;
	}

	public int getRotorSteps() {
		return rotorSteps;
	}

	@Override
	public String toString() {
		StringBuffer tempBuffer = new StringBuffer();
		int i = 1;
		for (MeasureValueCollector tempCollector : measurements) {
			tempBuffer.append("Messung");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(i++);
			tempBuffer.append("\n");
			tempBuffer.append("Startzeit");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(new Date(tempCollector.getMeasureStartTime()));
			tempBuffer.append("\n");
			tempBuffer.append("Anzahl der Messungen");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(tempCollector.getSize());
			tempBuffer.append("\n");
			tempBuffer.append("Dauer der Messungen");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(measureDuration);
			tempBuffer.append("\n");
			tempBuffer.append("Beste vom System ermittelte Messung");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(getBestMeasure());
			tempBuffer.append("\n");
			tempBuffer.append("Gap");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(tempCollector.getGap());
			tempBuffer.append("\n");
			tempBuffer.append("Gap");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(tempCollector.getGap2());
			tempBuffer.append("\n");
			tempBuffer.append("x");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(floatToString(tempCollector.getValuesOfX()));
			tempBuffer.append("\n");
			tempBuffer.append("y");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(floatToString(tempCollector.getValuesOfY()));
			tempBuffer.append("\n");
			tempBuffer.append("z");
			tempBuffer.append(SEPARATOR);
			tempBuffer.append(floatToString(tempCollector.getValuesOfZ()));
			tempBuffer.append("\n");
		}
		return tempBuffer.toString();
	}

	public List<MeasureValueCollector> getMeasurements() {
		return measurements;
	}

	public void setMeasurements(List<MeasureValueCollector> aMeasurements) {
		measurements = aMeasurements;
	}

	private String floatToString(float[] someValues) {
		StringBuffer tempBuffer = new StringBuffer();
		String tempSeparator = "";
		for  (int i=0; i<someValues.length; i++) {
			tempBuffer.append(tempSeparator);
			tempSeparator = SEPARATOR;
			tempBuffer.append(someValues[i]);
		}
		return tempBuffer.toString();
	}
}
